﻿using System.Collections.Generic;
using CID.Domain.Infrastructure;
using NailsFramework.Persistence;

namespace CID.Domain.Operations
{
    public class Location : Model<Location>, ILocation
    {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual IDeploymentAgent DeploymentAgent { get; set; }

        private IList<IServer> servers = new List<IServer>();
        public virtual IEnumerable<IServer> Servers { get { return servers; } }

        protected Location()
        {
        }

        public Location(string name, string description, IDeploymentAgent deploymentAgent)
        {
            Name = name;
            Description = description;
            if (deploymentAgent != null) deploymentAgent.AddLocation(this);
        }

        public virtual void AddServer(IServer server)
        {
            if (!servers.Contains(server))
            {
                if (server.Location != null) server.Location.RemoveServer(server);
                servers.Add(server);
                server.Location = this;
            }
        }

        public virtual void RemoveServer(IServer server)
        {
            if (servers.Contains(server))
            {
                servers.Remove(server);
                server.Location = null;
            }
        }
    }
}
